package com.yjhb.android.sbz.mvp.report;

import com.google.gson.Gson;
import com.yjhb.android.sbz.dialog.TipOffDialog;
import com.yjhb.android.sbz.entity.Report;
import com.yjhb.android.sbz.http.HttpObserver;
import com.yjhb.android.sbz.http.HttpTool;
import com.yjhb.android.sbz.http.UploadTool;
import com.yjhb.android.sbz.mvp.ConventionPresenter;
import com.jojo.android.zxlib.entity.Result;
import com.luck.picture.lib.entity.LocalMedia;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * 创建者 zx
 * 创建时间 2020/12/24 9:58
 */
public class ReportPresenter extends ConventionPresenter<ReportView> {
    private final String TAG = "ReportPresenter";

    @Override
    protected void onDestroy() {

    }

    public ReportPresenter(ReportView reportView) {
        super(reportView);
    }


    /**
     * 举报
     *
     * @param medias       图片
     * @param content      内容
     * @param targetUserId 目标
     * @param type         类型
     * @param businessId   业务ID
     */
    public void report(List<LocalMedia> medias, String content, Integer targetUserId, int type, String businessId) {
        TipOffDialog.DataBean dataBean = new TipOffDialog.DataBean();
        dataBean.setContent(content);
        if (view != null) {
            view.onUploadIng(medias.size(), 0);
        }
        UploadTool.OSSUploadImages(medias, false,  new Observer<String>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(String s) {
                if (dataBean.getUrls() == null) {
                    dataBean.setUrls(new ArrayList<>());
                }
                dataBean.getUrls().add(s);
                if (view != null) {
                    view.onUploadIng(medias.size(), dataBean.getUrls().size());
                }
            }

            @Override
            public void onError(Throwable e) {
                if (view != null) {
                    view.onFailure(202, e.getMessage());
                }
            }

            @Override
            public void onComplete() {

                try {
                    if (view != null) {
                        view.onUploadSuccess();
                    }
                    String content = new Gson().toJson(dataBean);
                    Report report = new Report();
                    report.setBizId(businessId);
                    report.setType(type);
                    report.setDefendant(targetUserId);
                    report.setPlaintiffContent(content);
                    HttpTool.request(model.prosecution(report), new HttpObserver<Object>() {
                        @Override
                        public void onResult(Object data) {
                            if (view != null) {
                                view.onSubmitSuccess();
                            }
                        }

                        @Override
                        public void onError(int code, String msg) {
                            if (view != null) {
                                view.onFailure(code, msg);
                            }
                        }
                    });
                } catch (Exception e) {
                    if (view != null) {
                        view.onFailure(-1, e.getMessage());
                    }
                }

            }
        });

    }


    /**
     * @param medias
     * @param content
     * @param report
     */
    public void refute(List<LocalMedia> medias, String content, Report report) {
        TipOffDialog.DataBean dataBean = new TipOffDialog.DataBean();
        dataBean.setContent(content);
        if (view != null) {
            view.onUploadIng(medias.size(), 0);
        }
        UploadTool.OSSUploadImages(medias, false,  new Observer<String>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(String s) {
                if (dataBean.getUrls() == null) {
                    dataBean.setUrls(new ArrayList<>());
                }
                dataBean.getUrls().add(s);
                if (view != null) {
                    view.onUploadIng(medias.size(), dataBean.getUrls().size());
                }
            }

            @Override
            public void onError(Throwable e) {
                if (view != null) {
                    view.onFailure(202, e.getMessage());
                }
            }

            @Override
            public void onComplete() {
                try {
                    if (view != null) {
                        view.onUploadSuccess();
                    }
                    String content = new Gson().toJson(dataBean);
                    Observable<Result<Object>> observable = null;
                    switch (report.getSubmitAgainState()) {
                        case 1:
                            observable = model.reSubmitReport(report.getId(), content);
                            break;
                        case 2:
                            observable = model.reRefuteContent(report.getId(), content);
                            break;
                        default:
                            observable = model.prosecutionRefute(report.getId(), content);
                    }
                    HttpTool.request(observable, new HttpObserver<Object>() {
                        @Override
                        public void onResult(Object data) {
                            if (view != null) {
                                view.onSubmitSuccess();
                            }
                        }

                        @Override
                        public void onError(int code, String msg) {
                            if (view != null) {
                                view.onFailure(code, msg);
                            }
                        }
                    });
                } catch (Exception e) {
                    if (view != null) {
                        view.onFailure(-1, e.getMessage());
                    }
                }

            }
        });
    }
}
